#!/bin/sh

stty -F /dev/ttyUSB0 speed 115200 cs8 -parenb -cstopb
$(/bin/ubus -t 600 -S call openmptcprouter status >./status.json 2>/dev/null)
$(cp ./status.json ./status2.json)
thermal_dir="/sys/class/thermal/"  
temp_file=""  
temp=0 
found_temp=false  
for subdir in "$thermal_dir"*/; do  
    subdir_name=$(basename "$subdir")  
    if [[ $subdir_name =~ ^thermal_zone[1-9]+ ]]; then  
        temp_file="$subdir/temp"  
        if [[ -r "$temp_file" ]]; then  
            temp_millidegrees=$(cat "$temp_file")  
            temp=$((temp_millidegrees / 1000))  
            found_temp=true  
            break 
        fi  
    fi  
done  
if $found_temp; then  
    echo -e -n "t37.txt=\"${temp}\"\xff\xff\xff" > /dev/ttyUSB0  
else  
    echo -e -n "t37.txt=\"0\"\xff\xff\xff" > /dev/ttyUSB0  
fi  
if $found_temp; then  
    echo -e -n "t37.txt=\"${temp}\"\xff\xff\xff" > /dev/ttyUSB0  
else  
    echo -e -n "t37.txt=\"0\"\xff\xff\xff" > /dev/ttyUSB0  
fi  
cpu=$(cat /proc/loadavg | awk '{print $1}')  
echo -e -n "t24.txt=\"${cpu}\"\xff\xff\xff" > /dev/ttyUSB0
memory_info=$(free)
available_memory=$(echo "$memory_info" | awk '/Mem:/{print $7}')
total_memory=$(echo "$memory_info" | awk '/Mem:/{print $2}')
division_result=$(echo "$available_memory $total_memory" | awk '{print $1/$2}')
usage_percentage=$(echo "scale=1;(1 - $division_result)*100" | bc)
usage_percentage_formatted=$(printf "%.1f%%" $usage_percentage)
echo -e -n "t38.txt=\"${usage_percentage_formatted%}\"\xff\xff\xff" > /dev/ttyUSB0
initial_stats=$(cat /proc/stat | grep 'cpu ')
sleep 1
later_stats=$(cat /proc/stat | grep 'cpu ')
initial_user=$(echo $initial_stats | awk '{print $2}')
initial_system=$(echo $initial_stats | awk '{print $4}')
initial_idle=$(echo $initial_stats | awk '{print $5}')
later_user=$(echo $later_stats | awk '{print $2}')
later_system=$(echo $later_stats | awk '{print $4}')
later_idle=$(echo $later_stats | awk '{print $5}')
total_initial=$(echo "$initial_user + $initial_system + $initial_idle" | bc)
total_later=$(echo "$later_user + $later_system + $later_idle" | bc)
diff_total=$((total_later - total_initial))
diff_idle=$((later_idle - initial_idle))
cpu_usage=$((100*(diff_total - diff_idle)/diff_total))
echo -e -n "t14.txt=\"${cpu_usage%}%\"\xff\xff\xff" > /dev/ttyUSB0
current_time=$(date +"%Y-%m-%d %H:%M")
echo -e -n "t3.txt=\"${current_time}\"\xff\xff\xff" > /dev/ttyUSB0
. /usr/share/libubox/jshn.sh
json_init
json_load_file ./status.json
json_select openmptcprouter
json_get_var service_addr_ip service_addr_ip
json_get_var total_traffic total_traffic
json_get_var vps_admin_error_msg vps_admin_error_msg
service_addr_ip=${service_addr_ip%.*}
echo -e -n "t23.txt=\"${service_addr_ip}\"\xff\xff\xff" > /dev/ttyUSB0
json_close_array
json_select wans
json_get_values vals
idx=$(echo "${vals}" |wc -w)
newidx=1
while [ $newidx -le $idx ]
do
    json_select $newidx
    json_get_var state gw_ping
    if [[ "$state" == "UP" ]];
    then
        json_get_var whois whois
        json_get_var status status
        json_get_var gateway gateway
        json_get_var tx tx
        json_get_var rx rx
        json_get_var latency latency
        latency=$((latency + 0))
        d_total=$((rx + tx))
        d_total=$((d_total / 1048576))
        echo -e -n "t$((69+newidx)).txt=\"${d_total}MB\"\xff\xff\xff" > /dev/ttyUSB0
        echo -e -n "t$((59+newidx)).txt=\" \"\xff\xff\xff" > /dev/ttyUSB0
        if [[ $whois == *"CMNET"* ]]; then
               echo -e -n "p$((2+newidx)).pic=14\xff\xff\xff" > /dev/ttyUSB0
        elif [[ $whois == *"CHINANET"* ]]; then
              echo -e -n "p$((2+newidx)).pic=12\xff\xff\xff" > /dev/ttyUSB0
        elif [[ $whois == *"UNICOM"* ]]; then
              echo -e -n "p$((2+newidx)).pic=13\xff\xff\xff" > /dev/ttyUSB0
        else 
              echo "Unknown network type."
        fi
        if [[ "$status" == "OK" ]]; then
               echo -e -n "p$((8+newidx)).pic=17\xff\xff\xff" > /dev/ttyUSB0
        elif [[ "$status" == "WARNING" ]]; then
              echo -e -n "p$((8+newidx)).pic=18\xff\xff\xff" > /dev/ttyUSB0
        elif [[ "$status" == "ERROR" ]]; then
              echo -e -n "p$((8+newidx)).pic=16\xff\xff\xff" > /dev/ttyUSB0
        else 
              echo " "
        fi
        echo -e -n "t$((89+newidx)).txt=\"${gateway}\"\xff\xff\xff" > /dev/ttyUSB0
        echo -e -n "t$((79+newidx)).txt=\"${latency}ms\"\xff\xff\xff" > /dev/ttyUSB0

    else
        echo -e -n "t$((59+newidx)).txt=\"No gateway\"\xff\xff\xff" > /dev/ttyUSB0
    fi
    newidx=$((newidx + 1))
    json_close_array
done

if [[ x"$vps_admin_error_msg" = x ]]; then
    echo -e -n "p2.pic=4\xff\xff\xff" > /dev/ttyUSB0
else
    echo -e -n "p2.pic=5\xff\xff\xff" > /dev/ttyUSB0
fi

if [[ x"$vps_admin_error_msg" = x ]]; then
    total_traffic=$(printf "%.1f\n" $(echo "scale=2;${total_traffic%.*}/1073741824" | bc)) 
    echo -e -n "t5.txt=\"${total_traffic}G\"\xff\xff\xff" > /dev/ttyUSB0
else 
   echo -e -n "t5.txt=\"x\"\xff\xff\xff" > /dev/ttyUSB0
fi


json_load_file ./status2.json
json_select wans
json_get_values vals
idx=`echo "${vals}" |wc -w`
S=2
for i in `seq $idx`
do
  json_select $i
  json_get_var ifname ifname
  json_get_var state gw_ping                                     
  if [ "$state"x == "UP"x ]                                             
  then  
   T=/sys/class/net/${ifname}/statistics/tx_bytes
   R=/sys/class/net/${ifname}/statistics/rx_bytes
  else
    T=/root/zero
    R=/root/zero
  fi
  eval X_${i}=`cat $T`
  eval M_${i}=`cat $R`
 json_close_array
done
sleep $S

function kb_to_mb(){
  let total=$1
  if [ 1024 -gt $total ]
     then 
        size="$total"Kb
   else
        size=`echo "scale=1; $total/1024" | bc`
        size="$size"Mb
  fi  
        echo $size

}


for i in `seq $idx`
do
  json_select $i
  json_get_var ifname ifname
  json_get_var state gw_ping
  if [ "$state"x == "UP"x ]
  then
   T=/sys/class/net/${ifname}/statistics/tx_bytes
   R=/sys/class/net/${ifname}/statistics/rx_bytes
  else
    T=/root/zero
    R=/root/zero
  fi
  eval Y_${i}=`cat $T`
  eval N_${i}=`cat $R`
 json_close_array
done
ATBPS=0
ARBPS=0
for i in `seq $idx`
do
    eval TBPS="$(((Y_${i}-X_${i})/S/128))"
    eval RBPS="$(((N_${i}-M_${i})/S/128))"
    echo -e -n "t$((39+i)).txt=\"$(kb_to_mb ${TBPS})\"\xff\xff\xff" > /dev/ttyUSB0
    echo -e -n "t$((49+i)).txt=\"$(kb_to_mb ${RBPS})\"\xff\xff\xff" > /dev/ttyUSB0
    let ATBPS+=${TBPS}
    let ARBPS+=${RBPS}
done
    echo -e -n "t4.txt=\"$(kb_to_mb ${ATBPS})\"\xff\xff\xff" > /dev/ttyUSB0
    echo -e -n "t15.txt=\"$(kb_to_mb ${ARBPS})\"\xff\xff\xff" > /dev/ttyUSB0